package com.xiaoshuidi.cloud.module.bpm.service.assignrecord;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xiaoshuidi.cloud.module.bpm.controller.admin.assignrecord.vo.TicketAssignRecordCreateReqVO;
import com.xiaoshuidi.cloud.module.bpm.convert.assignrecord.TicketAssignRecordConvert;
import com.xiaoshuidi.cloud.module.bpm.dal.dataobject.assignrecord.TicketAssignRecordDO;
import com.xiaoshuidi.cloud.module.bpm.dal.mysql.assignrecord.TicketAssignRecordMapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.List;

/**
 * 预约工单指派记录 Service 实现类
 *
 * @author 合房租赁
 */
@Service
@Validated
public class TicketAssignRecordServiceImpl implements TicketAssignRecordService {

    @Resource
    private TicketAssignRecordMapper ticketAssignRecordMapper;

    @Override
    public Long createTicketAssignRecord(TicketAssignRecordCreateReqVO createReqVO) {
        // 插入
        TicketAssignRecordDO ticketAssignRecord = TicketAssignRecordConvert.INSTANCE.convert(createReqVO);
        ticketAssignRecordMapper.insert(ticketAssignRecord);
        // 返回
        return ticketAssignRecord.getId();
    }

    @Override
    public void deleteByReserveTicketId(Long reserveTicketId) {
        LambdaQueryWrapper<TicketAssignRecordDO> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(TicketAssignRecordDO::getReserveTicketId, reserveTicketId);
        ticketAssignRecordMapper.delete(wrapper);
    }

    @Override
    public List<TicketAssignRecordDO> getByTicketIdOrderById(Long reserveTicketId) {
        LambdaQueryWrapper<TicketAssignRecordDO> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(TicketAssignRecordDO::getReserveTicketId, reserveTicketId);
        wrapper.orderByDesc(TicketAssignRecordDO::getId);
        return ticketAssignRecordMapper.selectList(wrapper);
    }

    /**
     * 查询工单的指派记录，按创建时间倒序
     * @param id
     * @return
     */
    @Override
    public List<TicketAssignRecordDO> getByTicketIdOrderByCreateTime(Long reserveTicketId) {
        LambdaQueryWrapper<TicketAssignRecordDO> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(TicketAssignRecordDO::getReserveTicketId, reserveTicketId);
        wrapper.orderByDesc(TicketAssignRecordDO::getCreateTime);
        return ticketAssignRecordMapper.selectList(wrapper);
    }


}
